package com.zhiwei.codesecurity.web.api.interfaces.controller;

import com.zhiwei.codesecurity.common.base.NullObjectResp;
import com.zhiwei.codesecurity.common.base.ServiceResponse;
import com.zhiwei.codesecurity.common.utils.BeanUtil;
import com.zhiwei.codesecurity.common.utils.IdWorker;
import com.zhiwei.codesecurity.common.utils.StringUtils;
import com.zhiwei.codesecurity.entity.Interfaces;
import com.zhiwei.codesecurity.service.interfaces.IInterfaceService;
import com.zhiwei.codesecurity.service.interfaces.request.QueryInterfaceReq;
import com.zhiwei.codesecurity.service.interfaces.response.QueryInterfaceServRlt;
import com.zhiwei.codesecurity.service.redis.IJedisClientUserService;
import com.zhiwei.codesecurity.web.api.interfaces.request.AddInterfaceReq;
import com.zhiwei.codesecurity.web.api.interfaces.request.DeleteInterfaceReq;
import com.zhiwei.codesecurity.web.api.interfaces.request.ModifyInterfaceReq;
import com.zhiwei.codesecurity.web.api.interfaces.response.AddInterfaceResp;
import com.zhiwei.codesecurity.web.common.base.BaseController;
import com.zhiwei.codesecurity.web.common.base.BaseResponse;
import com.zhiwei.codesecurity.web.common.base.RequestHead;
import com.zhiwei.codesecurity.web.common.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

@Controller
@RequestMapping("/interface")
public class InterfaceController extends BaseController {

    @Autowired
    @Qualifier("interfacesService")
    private IInterfaceService interfaceService;


    /**
     * 添加接口
     * @param head
     * @param addInterfaceReq
     * @param result
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public BaseResponse AddInterface(@RequestBody RequestHead head, @Valid @RequestBody AddInterfaceReq addInterfaceReq,
                                     BindingResult result) {
        logger.info(">>> process: 添加接口|interface_name={}", addInterfaceReq.getInterfaceName());
        if (ResponseUtil.existsParamsError(result)) {
            return ResponseUtil.getErrorParams(result);
        }

        Interfaces interfaces = new Interfaces();
        BeanUtil.copyPropertiesQuietly(addInterfaceReq, interfaces);
        interfaces.setInterfaceId(IdWorker.generate());

        String interfaceId = interfaceService.AddScanInterface(interfaces);
        if (StringUtils.isBlank(interfaceId)) {
            logger.warn("用户添加失败!");
            return ResponseUtil.getErrorBusi("用户添加失败");
        }

        AddInterfaceResp addInterfaceResp = new AddInterfaceResp();
        addInterfaceResp.setInterfaceId(interfaceId);

        return ResponseUtil.getSuccessResponse(addInterfaceResp);
    }

    /**
     * 修改接口
     * @param head
     * @param modifyInterfaceReq
     * @param result
     * @return
     */
    @RequestMapping("/modify")
    @ResponseBody
    public BaseResponse ModifyInterface(@RequestBody RequestHead head, @Valid @RequestBody ModifyInterfaceReq modifyInterfaceReq,
                                        BindingResult result) {
        logger.info(">>> process: 修改接口|interface_name={}", modifyInterfaceReq.getInterfaceId());
        if (ResponseUtil.existsParamsError(result)) {
            return ResponseUtil.getErrorParams(result);
        }

        Interfaces interfaces = new Interfaces();
        BeanUtil.copyPropertiesQuietly(modifyInterfaceReq, interfaces);
        ServiceResponse<NullObjectResp> serviceResponse = interfaceService.modifyScanInterface(interfaces);
        if (serviceResponse.isError()) {
            logger.warn("修改接口失败, returnCode={}, returnMsg={}", serviceResponse.getCode(), serviceResponse.getMsg());
            return ResponseUtil.getErrorBusi(serviceResponse.getMsg());
        }

        return ResponseUtil.getSuccessResponse();

    }

    /**
     * 删除接口
     * @param head
     * @param deleteInterfaceReq
     * @param result
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public BaseResponse DeleteInterface(@RequestBody RequestHead head, @Valid @RequestBody DeleteInterfaceReq deleteInterfaceReq,
                                        BindingResult result) {
        logger.info(">>> process: 删除接口|interface_id={}", deleteInterfaceReq.getInterfaceId());
        if (ResponseUtil.existsParamsError(result)) {
            return ResponseUtil.getErrorParams(result);
        }

        ServiceResponse<NullObjectResp> serviceResponse =
                interfaceService.deleteScanInterface(String.valueOf(deleteInterfaceReq.getInterfaceId()));
        if (serviceResponse.isError()) {
            logger.warn("删除接口失败, returnCode={}, returnMsg={}", serviceResponse.getCode(), serviceResponse.getMsg());
            return ResponseUtil.getErrorBusi(serviceResponse.getMsg());
        }

        return ResponseUtil.getSuccessResponse();
    }

    /**
     * 查询接口
     * @param head
     * @param queryInterfaceReq
     * @param result
     * @return
     */
    @RequestMapping("/query")
    @ResponseBody
    public BaseResponse QueryInterface(@RequestBody RequestHead head, @Valid @RequestBody QueryInterfaceReq queryInterfaceReq,
                                       BindingResult result) {
        logger.info(">>> process: 查询接口|queryInterfaceReq={}", queryInterfaceReq);
        if (ResponseUtil.existsParamsError(result)) {
            return ResponseUtil.getErrorParams(result);
        }

        ServiceResponse<QueryInterfaceServRlt> serviceResponse = interfaceService.queryScanInterfaceList(queryInterfaceReq);

        if (serviceResponse.isError()) {
            logger.warn("查询接口失败, returnCode={}, returnMsg={}", serviceResponse.getCode(), serviceResponse.getMsg());
            return ResponseUtil.getErrorBusi(serviceResponse.getMsg());
        }

        return ResponseUtil.getSuccessResponse(serviceResponse.getData());
    }

}
